package com.haixue.android.academy.view.tiku;

import java.io.File;

import org.xml.sax.XMLReader;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.text.Editable;
import android.text.Html.TagHandler;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.Toast;

public class MyTagHandler implements TagHandler {

	private Context context;
	
	public MyTagHandler(Context context) {
		this.context = context;
	}
	
	@Override
	public void handleTag(boolean opening, String tag, Editable output,
			XMLReader xmlReader) {
		// TODO Auto-generated method stub

		if (tag.toLowerCase().equals("img")) {
			
			int len = output.length();
			//System.out.println(len);
			ImageSpan[] images = output.getSpans(len-1, len, ImageSpan.class);
			String imgURL = images[0].getSource();
			System.out.println("imgURL"+imgURL);
			output.setSpan(new ImageClick(context, imgURL), len-1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
	}
	
	private class ImageClick extends ClickableSpan {

		private String url;
		private Context context;
		
		public ImageClick(Context context, String url) {
			
			this.context = context;
			this.url = url;
		}
		
		@Override
		public void onClick(View widget) {
			// TODO Auto-generated method stub
           
			String imageName = Common.md5(url);
			String sdcardPath = Environment.getExternalStorageDirectory().toString(); 
			String[] ss = url.split("\\.");
			
			if (ss.length==1) {
				
				return;
			}
			
			String ext = ss[ss.length - 1];
			
			String saveDir=sdcardPath + "/" + context.getPackageName();
			
			File fileDir=new File(saveDir);
			
			if (!fileDir.exists()) {
				fileDir.mkdirs();
			}
			
			String savePath = saveDir + "/" + imageName + "." + ext;
			
			File file=new File(savePath);
			
			if (!file.exists()) {
				return ;
				/*try {
					file.createNewFile();
				
					InputStream inputStream=context.getAssets().open(url);
				
					
					FileOutputStream fileOutputStream=new FileOutputStream(file);
					
					byte buffer[]=new byte[1024*100];
					
					int count=inputStream.read(buffer);
					
					while (count>0) {
						fileOutputStream.write(buffer, 0, count);
						count=inputStream.read(buffer);
					}
	
					inputStream.close();
					fileOutputStream.close();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				*/
			}
			
				
			if (file.exists()) {
               
              
				Intent intent = new Intent(context,ImageActivity.class);
				intent.setData(Uri.fromFile(file));
				
				context.startActivity(intent);
				//((Activity) context).overridePendingTransition(R.anim.zoom_exit, R.anim.zoom_enter); 
				/*intent.setAction(Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.fromFile(file), "image/*");
				context.startActivity(intent);*/
			
			}
	

		}
		
	}

}
